package com.aiss.ws.activity;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.aiss.ws.Entity.CurseItem;
import com.aiss.ws.Entity.TypeItem;
import com.aiss.ws.R;
import com.aiss.ws.adapter.CourseDetailAdapter;
import com.aiss.ws.httptools.GetJSONObjectPostUtil;
import com.aiss.ws.httptools.GetJsonListener;
import com.aiss.ws.utils.Command;
import com.aiss.ws.utils.Futil;
import com.aiss.ws.utils.Url;
import com.lidroid.xutils.exception.HttpException;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by chenqiang on 16/5/17.
 * 详情
 */
@EActivity(R.layout.activity_coursedetail)
public class CourseDetailActivity extends BaseActivity {

    private CurseItem item = new CurseItem();

    private Context context = this;

    private Handler handlerupdate = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                List<TypeItem> datas = item.getDatas();
                for(int i = 0 ; i < datas.size();i++){
                    if(datas.get(i).getChange()==1){
                        item.setBuy_type_id(datas.get(i).getId());
                        item.setBuy_type_name(datas.get(i).getName());
                        item.setBuy_type_price(datas.get(i).getPrice());
//                        item.setPrice(datas.get(i).getPrice());
//                        item.setClass_hour(datas.get(i).getClass_hour());
                        time.setText("\t\t学时:\t"+datas.get(i).getClass_hour()+"时");
                        money.setText("\t"+datas.get(i).getPrice()+"元");
                    }
                }
            }
        }
    };


    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==Command.HANDLECODE7){


                try {
                    JSONObject obj = (JSONObject) msg.obj;
                    JSONObject object = obj.getJSONObject("return_data");

                    JSONArray array = object.getJSONArray("type");
                    List<TypeItem> datas = new ArrayList<>();
                    for(int i = 0 ; i < array.length();i++){
                        TypeItem typeItem = new TypeItem();
                        typeItem.setId(array.getJSONObject(i).getString("id"));
                        typeItem.setClass_hour(array.getJSONObject(i).getString("class_hour"));
                        typeItem.setName(array.getJSONObject(i).getString("name"));
                        typeItem.setPrice(array.getJSONObject(i).getString("price"));
                        if(i==0){

                            typeItem.setChange(1);
                        }else{
                            typeItem.setChange(0);
                        }
                        datas.add(typeItem);
                    }
                    item.setDatas(datas);

                    item.setPrice(item.getDatas().get(0).getPrice());
                    item.setClass_hour(item.getDatas().get(0).getClass_hour());
                    item.setBuy_type_price(item.getDatas().get(0).getPrice());
                    item.setBuy_type_id(item.getDatas().get(0).getId());

                    item.setIdx(object.getString("idx"));
                    item.setNamex(object.getString("namex"));
                    item.setIs_buy(object.getString("is_buy"));
                    item.setRemaining_time(object.getString("remaining_time"));
                    item.setEnd_time(object.getString("end_time"));
                    item.setBuy_type_name(object.getString("buy_type_name"));
//                    item.setBuy_type_id(object.getString("buy_type_id"));
//                    item.setBuy_type_price(object.getString("buy_type_price"));

                    CourseDetailAdapter adapter = new CourseDetailAdapter(item.getDatas(),context,handlerupdate);
                    listView.setAdapter(adapter);

                    name.setText("课程名:\t"+object.getString("namex"));
                    time.setText("\t\t学时:\t"+item.getDatas().get(0).getClass_hour()+"时");
                    money.setText("\t"+item.getDatas().get(0).getPrice()+"元");

                    if(object.getString("is_buy").equals("1")){
                        linearlayout_button.setVisibility(View.GONE);
                        button3.setVisibility(View.VISIBLE);
                        listView.setVisibility(View.GONE);
                        grade_tv.setText("等级:\t"+item.getBuy_type_name());
                    }else{
                        grade_tv.setText("等级");
                        linearlayout_button.setVisibility(View.VISIBLE);
                        button3.setVisibility(View.GONE);
                        listView.setVisibility(View.VISIBLE);
                    }

                    coursedetail_ll.setVisibility(View.VISIBLE);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }else if(msg.what== Command.HANDLECODE9){
                try {
                    JSONObject obj = (JSONObject) msg.obj;
                    showMessage(obj.getString("return_data"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    };

    @ViewById(R.id.grade_tv)
    TextView grade_tv;

    @ViewById(R.id.name)
    TextView name;

    @ViewById(R.id.time)
    TextView time;
    @ViewById(R.id.money)
    TextView money;


    @ViewById(R.id.linearlayout_button)
    LinearLayout linearlayout_button;

    @ViewById(R.id.button3)
    TextView button3;

    @Click(R.id.back)
    void back(){
        finish();
    }

    @Extra("")
    String idx;

    @Extra("")
    String ftype;

    @ViewById(R.id.coursedetail_ll)
    LinearLayout coursedetail_ll;

    @ViewById(R.id.title)
    TextView title_tv;

    @ViewById(R.id.listView)
    ListView listView;




    @AfterViews
    void init(){
        title_tv.setText("课程详情");

    }

    /**
     * 加入购物车
     */
    @Click(R.id.button1)
    void setCar(){
        HashMap<String,String> map = new HashMap<>();
        map.put("type","add");
        map.put("idx",idx);
        Futil.xutils(Url.CART,map,handler,Command.HANDLECODE9);
//       CourseDetailFinderActivity_.intent(this).start();
    }

    /**
     * 立即购买
     */
    @Click(R.id.button2)
    void button2(){
        HashMap<String,String> map = new HashMap<>();
        map.put("type","is_completion");
        Futil.AddHashMap(map);
        new GetJSONObjectPostUtil(Url.INFORMATION, map, new GetJsonListener() {
            @Override
            public void onSuccessed(JSONObject result) {
                try {
                    if(result.getString("state").equals("1")){

                        if(result.getString("return_data").equals("1")){
                            AgreenOnActivity_.intent(context).extra("item",item).start();
                        }else{
                            AddInformationctivity_.intent(context).extra("item",item).start();
                        }


                    }else{
                        showMessage(result.getString("return_data"));

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailed(HttpException error, String msg) {
                showMessage("请检查网络连接");
            }
        });

    }


    /**
     * 查看课程
     */
    @Click(R.id.button3)
    void button3(){
        CourseDetailFinderActivity_.intent(this).extra("idx",idx).extra("ftype",ftype).start();
    }

    @Override
    protected void onResume() {
        super.onResume();
        HashMap<String,String> map = new HashMap<>();
        map.put("type","info");
        map.put("idx",idx);
        Futil.xutils(Url.COURSE,map,handler,Command.HANDLECODE7);
    }


}
